/*
 * @Author: 码上talk|RC
 * @Date: 2021-06-22 14:30:48
 * @LastEditTime: 2022-06-29 11:06:55
 * @LastEditors: 码上talk|RC
 * @Description: 
 * @FilePath: /lejifen-ma/store/user/actions.js
 * @微信:  13680065830
 * @邮箱:  3189482282@qq.com
 * @oops: Just do what I think it is right
 */

import * as types from './mutation-types';
import { localCache } from '@/utils/localCache';
import { model } from '@/model';
const { Request } = model;
const { Member } = model.collection;

export const actions = {
  setMaInitConfig: ({ dispatch, commit },query) => {
   return new Request().do('ma', 'ma', 'initConfig', {
     params: {}, data: { query }
    }).then(res => {
      const { status, data } = res;
      if (status && status !== 500) {
        uni.setStorageSync('province', data.region.provinceId);
        uni.setStorageSync('city', data.region.cityId);
        uni.setStorageSync('district', data.region.districtId);
        commit(types.SET_MEMBER_ADDRESS_LIST, data.memberAddressList);
      }
    });
  },
  getUserInfo: ({ dispatch, commit }) => {
    return Member.sendApi('info', { params: {}, data: {} }, { parse4Entity: true }).then(res => {
      const { status, data } = res;
      if (status) {
        commit('SET_IS_LOGIN', true, { root: true });
        commit(types.SET_MEMBER_INFO, data);
      }
    })
  },
  logout: ({ commit }) => {
    return new Promise((resolve) => {
      localCache.clearToken();
      commit('SET_IS_LOGIN', false, { root: true });
      commit(types.SET_MEMBER_INFO, new Member());
      resolve();
    });
  }
};
